package com.ssh.tts.cui;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Scanner;

import com.ssh.tts.model.IUser;
import com.ssh.tts.model.Iou;
import com.ssh.tts.model.Train;
import com.ssh.tts.service.IouService;
import com.ssh.tts.service.TrainService;

public class TrainCUI {
	
	private static TrainService trainService=new TrainService();
	private static IouService iouService = new IouService();
	
	public static void trainQuery() {
		System.out.println("车次查询，请按照要求输入查询关键字：");
		Scanner input=new Scanner(System.in);
		System.out.print("出发地：");
		String fromAddress=input.nextLine();
		System.out.print("目的地：");
		String toAddress=input.nextLine();
		System.out.print("发车日期：");
		String date=input.nextLine();
		System.out.print("车次：");
		String trainId=input.nextLine();
		
		Train train=new Train(0,trainId,toAddress,fromAddress,date,null,null);
		
		List<Train> list=trainService.queryTrain(train);
		System.out.println("查询结果:");
		if(list.size()==0) {
			System.out.print("没有查询到相关数据！");
		}
		list.forEach((train0)->{
			System.out.println(train0.toString());
		});
		//input.close();
	}
	/**
	 * 购票
	 * @param user
	 */
	public static void buyTrain(List<IUser> user) {
		Scanner input=new Scanner(System.in);
		System.out.print("请输入想购买车票的车次：");
		String trainId=input.nextLine();
		List<Train> train = trainService.queryTrainByTrainId(trainId);
		System.out.print("请输入想车票的数量：");
		int num = input.nextInt();
		
		LocalDate ld = LocalDate.now();
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		
		try {
			iouService.addIou(new Iou(0, String.valueOf(user.get(0).getUSER_ID()), trainId, dtf.format(ld), num, num*train.get(0).getTRAIN_FARES()));
			System.out.println("购票成功！");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	
	/**
	 * 根据用户id查询订单
	 * @param user
	 */
	public static void queryByUser_id(List<IUser> user) {
		System.out.println("查询结果:");
		try {
			List<Iou> list=iouService.queryByUser_id(user.get(0).getUSER_ID());
			list.forEach((train0)->{
				System.out.println(train0.toString());
			});
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		
	}
	/**
	 * 退票
	 * @param user
	 */
	public static void refundTrain(List<IUser> user) {
		System.out.println("请输入要退票的车次：");
		Scanner sc = new Scanner(System.in);
		String trainId = sc.nextLine();
		try {
			iouService.deleteIou(new Iou(0,String.valueOf(user.get(0).getUSER_ID()),trainId,"",0,0));
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	} 
	
}
